package com.hnyyac.ad.mds.views_load


import android.app.Activity
import android.content.Context
import android.util.Log
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager
import com.hnyyac.ad.mds.GMAds
import com.hnyyac.ad.mds.IAdType
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.KadControler


class NewGromoreOnInterstitialAd(
    val activity: Activity, val showId: String,
    override var ecpmAdnShowIdListStr: String? = null,
    override var showChainSession: String? = null,
    var initialPosition: Int = 0 // 新增构造参数
) : IAdView {
    private val TAG = "qin"
    private var mInterstitialAd: TTFullScreenVideoAd? = null
    private var mUAdViewShowListener: IAdViewListener? = null

    var adNativeLoader: TTAdNative?=null
    var adslot: AdSlot?=null


    override fun getPosition(): Int {
        return initialPosition
    }

    override fun getSdkName(): String? {
        return mInterstitialAd?.mediationManager?.showEcpm?.sdkName.toString()
    }

    override fun setPosition(position: Int) {
        this.initialPosition = position
    }

    /**
     * 发起 load 场景
     */


    override fun loadAd() {
        Log.i(TAG, "开始加载插屏广告----------"+initialPosition)

        createAdSlot()
        attemptLoadAd()
    }
    private fun createAdSlot() {
        adNativeLoader =
            TTAdSdk.getAdManager().createAdNative(activity) //需要回传activity，切记！！！
        adslot = AdSlot.Builder()
            .setCodeId(showId)
            .setOrientation(TTAdConstant.ORIENTATION_VERTICAL) //设置横竖屏方向
            .setMediationAdSlot(
                MediationAdSlot.Builder()
                    .setMuted(true) //是否静音
                    .setVolume(0.7f) //设置音量
                    .setBidNotify(true) //竞价结果通知
                    .build()
            )
            .build()
    }

    private fun attemptLoadAd() {
        Log.i("qin","开始拿插屏广告")
        GMAds.iAdViewListener?.onTrigger(this@NewGromoreOnInterstitialAd)
        adNativeLoader?.loadFullScreenVideoAd(adslot, object : TTAdNative.FullScreenVideoAdListener {
            override fun onError(p0: Int, p1: String?) {
                GMAds.iAdViewListener?.onError(p1.toString())


            }

            override fun onFullScreenVideoAdLoad(p0: TTFullScreenVideoAd?) {
                mInterstitialAd = p0
                if (mInterstitialAd != null && mInterstitialAd?.mediationManager?.isReady!!) {
                    KadControler.pushUAdViewCache(this@NewGromoreOnInterstitialAd)
                    // Log.i(TAG, "拿插屏广告成功...")
                    //PreLoadHelper.pushPreLoadAd(this@NewGromoreOnInterstitialAd)
//                    mUAdViewShowListener?.onSuccess()
                    GMAds.iAdViewListener?.onLoad(this@NewGromoreOnInterstitialAd)

                } else {
                    //  Log.i(TAG, "拿插屏广告失败...")
                    mUAdViewShowListener?.onLoadError(this@NewGromoreOnInterstitialAd)
                    GMAds.iAdViewListener?.onLoadError(this@NewGromoreOnInterstitialAd)

                }
            }

            override fun onFullScreenVideoCached() {

            }

            override fun onFullScreenVideoCached(p0: TTFullScreenVideoAd?) {

            }

        })
    }

    override fun getAdType(): IAdType {
        return IAdType.interstitial
    }

    override fun getEcpm(): Double {
        kotlin.runCatching {
            return getEcpm_chapin(mInterstitialAd!!).toDouble()
        }
        return 0.0
    }

    override fun show(activity: Activity) {
        Log.i(TAG, "开始加载插屏广告show----------"+initialPosition)
        // 展示广告
        this.mInterstitialAd?.setFullScreenVideoAdInteractionListener(object :
            TTFullScreenVideoAd.FullScreenVideoAdInteractionListener {
            override fun onAdShow() {
                mUAdViewShowListener?.onAdShow(this@NewGromoreOnInterstitialAd)
                GMAds.iAdViewListener?.onAdShow(this@NewGromoreOnInterstitialAd)

            }

            override fun onAdVideoBarClick() {
                mUAdViewShowListener?.onAdClick(this@NewGromoreOnInterstitialAd)
                GMAds.iAdViewListener?.onAdClick(this@NewGromoreOnInterstitialAd)
            }

            override fun onAdClose() {
                Log.e("NewGromoreOnInterstitialAd","onAdClose")
                mUAdViewShowListener?.onAdClose( this@NewGromoreOnInterstitialAd)
                GMAds.iAdViewListener?.onAdClose( this@NewGromoreOnInterstitialAd)

            }

            override fun onVideoComplete() {
                mUAdViewShowListener?.onSuccess(this@NewGromoreOnInterstitialAd)
            }

            override fun onSkippedVideo() {
                mUAdViewShowListener?.onSkipped()
                GMAds.iAdViewListener?.onSkipped()
            }

        })
        mInterstitialAd?.showFullScreenVideoAd(activity)
    }


    override fun getAdShowId(): String {
        return showId
    }

    override fun getSlotId(): String {
        return mInterstitialAd?.mediationManager?.showEcpm?.slotId.toString()
    }

    override fun isValid(): Boolean {
        return mInterstitialAd!!.mediationManager.isReady
    }

    override fun getshowid_bg(): String {
        return mInterstitialAd?.mediationManager?.showEcpm?.requestId.toString()
    }

    override fun toString(): String {
//        return "NewTopOnInterstitialAd(${getEcpm_chapin(mInterstitialAd!).let {
//            "ecpm:${it.ecpm},adsource_id:${it.adsourceId},networkPlacementId:${it.networkPlacementId}"
//        } ?: "$showId"})"
        return "";
    }

    override fun setOnShowingListener(listener: IAdViewListener?) {
        this.mUAdViewShowListener = listener
    }




    /**
     * 获取插屏广告的ecpma
     */
    private fun getEcpm_chapin(ad: TTFullScreenVideoAd): String {
        val mediationManager: MediationBaseManager? = ad.mediationManager
        if (mediationManager != null) {
            val showEcpm = mediationManager.showEcpm
            if (showEcpm != null) {
                return showEcpm.ecpm
            }
        }
        return "0.0"
    }
}